/**
 * 标准分页功能使用示例
 *
 * 新的分页函数返回数据结构：
 * {
 *   data: [],           // 数据数组
 *   page: 1,           // 当前页码
 *   pageSize: 10,      // 每页数量
 *   total: 100,        // 总数据条数
 *   totalPage: 10      // 总页数
 * }
 */

import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, Like } from 'typeorm'
import { standardPaginate, StandardPagination } from '~/helper/paginate'
import { PagerDto } from '~/common/dto/pager.dto'

// 假设这是您的实体类
class SampleEntity {
  id: number
  name: string
  description: string
}

// 查询DTO示例
class SampleQueryDto extends PagerDto {
  name?: string
  description?: string
}

@Injectable()
export class SampleService {
  constructor(
    @InjectRepository(SampleEntity)
    private readonly sampleRepository: Repository<SampleEntity>,
  ) {}

  /**
   * 使用Repository进行分页查询
   */
  async listWithRepository({
    page,
    pageSize,
    name,
    description,
  }: SampleQueryDto): Promise<StandardPagination<SampleEntity>> {
    const searchOptions = {
      where: {
        ...(name && { name: Like(`%${name}%`) }),
        ...(description && { description: Like(`%${description}%`) }),
      },
      order: {
        id: 'DESC' as const,
      },
    }

    return standardPaginate(this.sampleRepository, { page, pageSize }, searchOptions)
  }

  /**
   * 使用QueryBuilder进行分页查询
   */
  async listWithQueryBuilder({
    page,
    pageSize,
    name,
    description,
  }: SampleQueryDto): Promise<StandardPagination<SampleEntity>> {
    const queryBuilder = this.sampleRepository
      .createQueryBuilder('sample')
      .select(['sample.id', 'sample.name', 'sample.description'])
      .orderBy('sample.id', 'DESC')

    if (name) {
      queryBuilder.andWhere('sample.name LIKE :name', { name: `%${name}%` })
    }

    if (description) {
      queryBuilder.andWhere('sample.description LIKE :description', {
        description: `%${description}%`
      })
    }

    return standardPaginate(queryBuilder, { page, pageSize })
  }

  /**
   * 复杂查询示例 - 带关联表
   */
  async listWithJoins({
    page,
    pageSize,
  }: PagerDto): Promise<StandardPagination<SampleEntity>> {
    const queryBuilder = this.sampleRepository
      .createQueryBuilder('sample')
      .leftJoinAndSelect('sample.category', 'category')
      .leftJoinAndSelect('sample.tags', 'tags')
      .where('sample.isActive = :isActive', { isActive: true })
      .orderBy('sample.createdAt', 'DESC')

    return standardPaginate(queryBuilder, { page, pageSize })
  }
}

/**
 * 控制器使用示例
 */
// @Controller('samples')
// export class SampleController {
//   constructor(private readonly sampleService: SampleService) {}

//   @Get()
//   @ApiOperation({ summary: '获取示例列表' })
//   @ApiResponse({
//     status: 200,
//     description: '返回分页数据',
//     schema: {
//       type: 'object',
//       properties: {
//         data: { type: 'array', items: { $ref: '#/components/schemas/SampleEntity' } },
//         page: { type: 'number', example: 1 },
//         pageSize: { type: 'number', example: 10 },
//         total: { type: 'number', example: 100 },
//         totalPage: { type: 'number', example: 10 }
//       }
//     }
//   })
//   async list(@Query() dto: SampleQueryDto): Promise<StandardPagination<SampleEntity>> {
//     return this.sampleService.listWithQueryBuilder(dto)
//   }
// }

/**
 * 前端接收到的数据结构示例：
 *
 * GET /samples?page=1&pageSize=10&name=测试
 *
 * Response:
 * {
 *   "data": [
 *     {
 *       "id": 1,
 *       "name": "测试数据1",
 *       "description": "这是测试描述"
 *     },
 *     {
 *       "id": 2,
 *       "name": "测试数据2",
 *       "description": "这是另一个测试描述"
 *     }
 *   ],
 *   "page": 1,
 *   "pageSize": 10,
 *   "total": 25,
 *   "totalPage": 3
 * }
 */